import { Injectable } from '@nestjs/common';
import { CreateRoleDto } from '../role/role.dto';
import { DeleteResult, EntityManager, Repository } from 'typeorm';
import { Role } from '../role/role.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Posts } from './posts.entity';
import { CreatePostsDto } from './posts.dto';
@Injectable()
export class PostsService {
  constructor (
    @InjectRepository(Posts)
    private readonly postsRepository: Repository<Posts>
  ) { }
  // 创建数据,传递一个对象类型的数据
  async create(data: CreatePostsDto, manager: EntityManager,) {
    const { name, title, content} = data;
    const role: { [propName: string]: any } = await manager.save(
      Posts,
      {
        name,
        title,
        content,
      },
    );
    return '创建成功';
  }

  // 查询全部的数据
  async list(): Promise<Posts[]> {
    return await this.postsRepository.find();
  }

  /**
   * 删除数据
   * @param id
   */
  async delete(id): Promise<DeleteResult> {
    return await this.postsRepository.delete(id);
  }

  /**
   * 根据id查找数据
   * @param ids
   */
  async findById(ids:string[]): Promise<Posts[]> {
    return await this.postsRepository.findByIds(ids);
  }
}
